package com.lemon.tuvi2015;

import java.util.ArrayList;
import java.util.Calendar;

import org.json.JSONObject;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;

import com.lemon.tuvi2015.object.GlobalObject;
import com.lemon.tuvi2015.util.BaseEngine;
import com.lemon.tuvi2015.util.JsonParser;
import com.lemon.tuvi2015.util.TagName;
import com.lemon.tuvi2015.view.CustomActionbar;

public class XongdatActivity extends Activity {
	CustomActionbar actionbar;
	ArrayList<GlobalObject> listZodiac;
	EditText editTextHoten,editTextNgaysinh;
	Button buttonTim;
	View main;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.xongdat_layout);
		initView();
		initActionbar();
	}

	void initView() {
		main=findViewById(R.id.main);
		editTextHoten=(EditText) findViewById(R.id.editText_hoten);
		editTextNgaysinh=(EditText) findViewById(R.id.editText_ngaysinh);
		buttonTim=(Button) findViewById(R.id.buttonTim);
		editTextNgaysinh.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					showDatedialog();
				}
			}
		});
		editTextNgaysinh.setOnClickListener(clickListener);
		buttonTim.setOnClickListener(clickListener);
	}

	OnClickListener clickListener=new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.editText_ngaysinh:
				showDatedialog();
				break;
			case R.id.buttonTim:
				String name=editTextHoten.getText().toString();
				String birthday=editTextNgaysinh.getText().toString();
				if(!name.equals("")&& !birthday.equals(""))
					new AsyncTaskParseJson(XongdatActivity.this).execute("");
				break;
			default:
				break;
			}
		}
	};
	public void showDatedialog() {
		final Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH);
		int day = c.get(Calendar.DAY_OF_MONTH);
		if(!editTextNgaysinh.getText().toString().equals("")){
			String[] dates=editTextNgaysinh.getText().toString().split("/");
			year = Integer.parseInt(dates[2]);
			month = Integer.parseInt(dates[1])-1;
			day = Integer.parseInt(dates[0]);
		}
		DatePickerDialog dpd = new DatePickerDialog(XongdatActivity.this, new DatePickerDialog.OnDateSetListener() {
			@Override
			public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
				editTextNgaysinh.setText(checkDateMonth(dayOfMonth) + "/" + checkDateMonth((monthOfYear + 1)) + "/" + year);
			}
		}, year, month, day);
		dpd.show();
	}
	public String checkDateMonth(int number){
		String num=Integer.toString(number);
		if(num.length()<2) return "0"+num;
		else return num;
	}
	void initActionbar() {
		actionbar = (CustomActionbar) findViewById(R.id.header);
		OnClickListener listenerBack = new OnClickListener() {

			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		};
		OnClickListener listenerShare = new OnClickListener() {

			@Override
			public void onClick(View v) {
				BaseEngine.shareBitmap(XongdatActivity.this,  BaseEngine.ConvertViewtoBitmap(XongdatActivity.this, main));
			}
		};
		actionbar.setActionbar(R.string.home_menu_xongdat, listenerBack, listenerShare);
	}

	public class AsyncTaskParseJson extends AsyncTask<String, String, String> {
		Context context;
		ProgressDialog progressDialog;
		public AsyncTaskParseJson(Context context) {
			this.context=context;
			progressDialog=new ProgressDialog(context);
		}
		@Override
		protected void onPreExecute() {
			if (!progressDialog.isShowing()) {
				progressDialog.show();
			}
		}

		@Override
		protected String doInBackground(String... arg0) {
			String str = JsonParser.xongdat(editTextHoten.getText().toString(), editTextNgaysinh.getText().toString());
			return str;
		}

		@Override
		protected void onPostExecute(String result) {
			if (progressDialog.isShowing()) {
				progressDialog.hide();
			}
			if (result != null) {
				BaseEngine.showLogformLongString("", result);
				try {
					if(!JsonParser.readStatus(result).equals("")){
						Intent intent = new Intent(XongdatActivity.this, XongdatDetailsActivity.class);
						intent.putExtra(TagName.KEY, result);
						startActivity(intent);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
}
